package clickhouse;

import clickhouse.service.impl.ClickHouseWrap;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import java.util.*;

/**
 * clickhouse jdbc增删改查接口测试
 * 脚本见附件clickhouse-sql2.txt
 */
class ClickHouseUtilTest2 {

    @Test
    void getListMapFromSql() {
        List<Map<String, Object>> r= ClickHouseUtil.getListMapFromSql("select * from temp");
        System.out.println(r);
    }
    @Test
    void testByDB() {
        List<Map<String,Object>> map=new ArrayList<>();
        for(int i=0;i<2;i++){
            Map<String,Object> d=new HashMap<>();
            d.put("id", UUID.randomUUID().toString());
            d.put("amount",new BigDecimal("542.215"));
            d.put("dateColulmn", new Date());
            d.put("dateTimeClumn",new Date());
            d.put("int16Column",i);
            d.put("uint64Column",(i+1)*10254152269L);
            map.add(d);
        }
        ClickHouseWrap clickHouseWrap=ClickHouseUtil.getClickHouseWrap();
        ClickHouseUtil.batchInsert(clickHouseWrap,"temp",map);
        ClickHouseUtil.batchInsert(clickHouseWrap,"temp",map);
        ClickHouseUtil.deleteData(clickHouseWrap,"temp","int16Column >0");
        List<Map<String, Object>> r= ClickHouseUtil.getListMapFromSql(clickHouseWrap,"select * from temp");
        System.out.println(r);
        ClickHouseUtil.closeDataBase(clickHouseWrap);
    }
    @Test
    void batchInsert() {
        List<Map<String,Object>> map=new ArrayList<>();
        for(int i=0;i<2;i++){
            Map<String,Object> d=new HashMap<>();
            d.put("id", UUID.randomUUID().toString());
            d.put("amount",new BigDecimal("542.215"));
            d.put("dateColulmn", new Date());
            d.put("dateTimeClumn",new Date());
            d.put("int16Column",i);
            d.put("uint64Column",(i+1)*10254152269L);
            map.add(d);
        }
        ClickHouseUtil.batchInsert("temp",map);
    }

    @Test
    void deleteData() {
        ClickHouseUtil.deleteData("temp","int16Column >0");
    }
    @Test
    void updateDataByDb() {
        String clause="int16Column =0";
        Map<String, Object> updateValues=new HashMap<>();
        updateValues.put("amount",new BigDecimal("88.00"));
        ClickHouseWrap clickHouseWrap=ClickHouseUtil.getClickHouseWrap();

        ClickHouseUtil.updateData(clickHouseWrap,"temp",updateValues,clause);
        updateValues.clear();
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(new Date());
        rightNow.add(Calendar.YEAR,-1);//日期减1年
        rightNow.add(Calendar.MONTH,3);//日期加3个月
        rightNow.add(Calendar.DAY_OF_YEAR,10);//日期加10天
        updateValues.put("dateColulmn",rightNow.getTime());
        updateValues.put("dateTimeClumn",rightNow.getTime());
        ClickHouseUtil.updateData(clickHouseWrap,"temp",updateValues,clause);

        ClickHouseUtil.closeDataBase(clickHouseWrap);

    }
    @Test
    void updateData() {
        String clause="int16Column =0";
        Map<String, Object> updateValues=new HashMap<>();
        updateValues.put("amount",new BigDecimal("88.00"));
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(new Date());
        rightNow.add(Calendar.YEAR,-1);//日期减1年
        rightNow.add(Calendar.MONTH,3);//日期加3个月
        rightNow.add(Calendar.DAY_OF_YEAR,10);//日期加10天

        updateValues.put("dateColulmn",rightNow.getTime());
        updateValues.put("dateTimeClumn",rightNow.getTime());
        ClickHouseUtil.updateData("temp",updateValues,clause);
    }

}